'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    // 检查是否已经有默认快捷键
    const [existingShortcuts] = await queryInterface.sequelize.query(
      'SELECT COUNT(*) as count FROM user_shortcuts'
    );
    
    // 如果表为空，则添加默认快捷键
    if (existingShortcuts[0].count === 0) {
      const defaultShortcuts = [
        {
          user_id: 1, // 默认用户ID
          type: 'global',
          module_id: null,
          key: 'Ctrl+D',
          action: '#/dashboard',
          description: '跳转到数据概览',
          created_at: new Date(),
          updated_at: new Date()
        },
        {
          user_id: 1,
          type: 'global',
          module_id: null,
          key: 'Ctrl+R',
          action: '#/reconciliation/workbench',
          description: '跳转到智能对账',
          created_at: new Date(),
          updated_at: new Date()
        },
        {
          user_id: 1,
          type: 'global',
          module_id: null,
          key: 'Ctrl+O',
          action: '#/data/orders',
          description: '跳转到销售订单',
          created_at: new Date(),
          updated_at: new Date()
        },
        {
          user_id: 1,
          type: 'global',
          module_id: null,
          key: 'Ctrl+T',
          action: '#/data/transactions',
          description: '跳转到银行流水',
          created_at: new Date(),
          updated_at: new Date()
        },
        {
          user_id: 1,
          type: 'global',
          module_id: null,
          key: 'Ctrl+C',
          action: '#/data/customers',
          description: '跳转到客户管理',
          created_at: new Date(),
          updated_at: new Date()
        },
        {
          user_id: 1,
          type: 'global',
          module_id: null,
          key: 'F5',
          action: 'reload',
          description: '刷新页面',
          created_at: new Date(),
          updated_at: new Date()
        },
        {
          user_id: 1,
          type: 'global',
          module_id: null,
          key: 'Ctrl+Shift+T',
          action: 'toggleTheme',
          description: '切换主题',
          created_at: new Date(),
          updated_at: new Date()
        },
        {
          user_id: 1,
          type: 'global',
          module_id: null,
          key: 'Ctrl+B',
          action: 'toggleSidebar',
          description: '切换侧边栏',
          created_at: new Date(),
          updated_at: new Date()
        }
      ];
      
      await queryInterface.bulkInsert('user_shortcuts', defaultShortcuts);
      console.log('默认快捷键已添加');
    } else {
      console.log('快捷键数据已存在，跳过初始化');
    }
  },

  down: async (queryInterface, Sequelize) => {
    // 删除所有默认快捷键
    await queryInterface.bulkDelete('user_shortcuts', {
      user_id: 1,
      key: {
        [Sequelize.Op.in]: ['Ctrl+D', 'Ctrl+R', 'Ctrl+O', 'Ctrl+T', 'Ctrl+C', 'F5', 'Ctrl+Shift+T', 'Ctrl+B']
      }
    });
  }
};